home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / Eenie / UEenie.cp < prev    next >
Encoding:
Text File  |  1995-02-13  |  11.4 KB  |  498 lines  |  [TEXT/MPS ]

  1. // UEenie.cp --------------------------------------------------------------------------
  2. // Copyright © 1991 by Apple Computer, Inc. All rights reserved.
  3.  
  4.  
  5. // INCLUDES ---------------------------------------------------------------------------
  6.  
  7. #include "EenieRez.h"                                        // combined header file for resource IDs
  8. #include "UEenie.h"                                            // class definitions
  9.  
  10.  
  11. // CLASS METHODS ----------------------------------------------------------------------
  12.  
  13. // TAdornerApplication ----------------------------------------------------------------
  14.  
  15. #undef Inherited
  16. #define Inherited TApplication
  17.  
  18. #pragma segment AInit
  19. DefineClass(TAdornerApplication, Inherited);
  20.  
  21. void TAdornerApplication::IAdornerApplication(OSType fileType, OSType creator)
  22. {
  23.     Inherited::IApplication(fileType, creator);
  24.  
  25.         // so the linker doesn't dead strip class info 
  26.         macroDontDeadStrip(TEenieView1);
  27.         macroDontDeadStrip(TEenieView2);
  28.         macroDontDeadStrip(TEenieView3);
  29.         macroDontDeadStrip(TEenieView4);
  30.         macroDontDeadStrip(TGrayFill);
  31.         macroDontDeadStrip(TOtherGrayFill);
  32.         macroDontDeadStrip(TEtchedFrame);
  33.  
  34.         RegisterStdType("TGrayFill", 'gray');                // register adorner types
  35.         RegisterStdType("TOtherGrayFill", 'dgry');                
  36.         RegisterStdType("TEtchedFrame", 'etch');
  37. }
  38.  
  39.  
  40. #pragma segment AOpen
  41.      TDocument* TAdornerApplication::DoMakeDocument(CommandNumber /* itsCmdNumber */,
  42.                                                       TFile*    /* itsFile */)
  43. {
  44.     FailInfo fi;
  45.     TAdornerDocument* anAdornerDocument;
  46.     
  47.     Try(fi) {
  48.         anAdornerDocument = new TAdornerDocument;
  49.         anAdornerDocument->IAdornerDocument();
  50.         fi.Success();
  51.     }
  52.     else
  53.         fi.ReSignal();
  54.  
  55.     return anAdornerDocument;
  56.  
  57. }
  58.  
  59.  
  60. // TAdornerDocument Methods -----------------------------------------------------------
  61.  
  62. #undef Inherited
  63. #define Inherited TDocument
  64.  
  65. #pragma segment AInit
  66. DefineClass(TAdornerDocument, Inherited);
  67.  
  68.  
  69.      void TAdornerDocument::IAdornerDocument()
  70. {
  71.     Inherited::IDocument();
  72. }
  73.  
  74.  
  75. #pragma segment AOpen
  76.      void TAdornerDocument::DoMakeViews(Boolean /*forPrinting*/)
  77. {
  78.     TWindow*        aWindow        = NULL;
  79.     TEenieView1*    view1        = NULL;
  80.     TEenieView2*    view2        = NULL;
  81.     TEenieView3*    view3        = NULL;
  82.     TEenieView4*    view4        = NULL;
  83.  
  84.     FailInfo fi;
  85.     
  86.     Try(fi) {
  87.         // create Window    
  88.         aWindow = gViewServer->NewTemplateWindow(1002, this);
  89.     
  90.         // add window adorner
  91.         aWindow->AddAdorner(NewStdAdorner('dgry',"",'dgry', kFreeOnDeletion), 
  92.                                             kAdornLast, kRedraw);
  93.     
  94.         // create views
  95.         view1   =(TEenieView1 *) aWindow->FindSubView('VW01'); FailNIL(view1);
  96.         view2   =(TEenieView2 *) aWindow->FindSubView('VW02'); FailNIL(view2);
  97.         view3   =(TEenieView3 *) aWindow->FindSubView('VW03'); FailNIL(view3);
  98.         view4   =(TEenieView4 *) aWindow->FindSubView('VW04'); FailNIL(view4);
  99.  
  100.         // add adorners to the new views
  101.         view1->AddAdorner(NewStdAdorner('gray',"",'gray', kFreeOnDeletion), 
  102.                             kAdornFirst, kRedraw);
  103.         view2->AddAdorner(NewStdAdorner('gray',"",'gray', kFreeOnDeletion), 
  104.                             kAdornFirst, kRedraw);
  105.         view3->AddAdorner(NewStdAdorner('gray',"",'gray', kFreeOnDeletion), 
  106.                             kAdornFirst, kRedraw);
  107.         view4->AddAdorner(NewStdAdorner('gray',"",'gray', kFreeOnDeletion), 
  108.                             kAdornFirst, kRedraw);
  109.         
  110.         // create dependencies
  111.         view1->AddDependent(view4); view2->AddDependent(view3);
  112.         view3->AddDependent(view2); view4->AddDependent(view1);
  113.  
  114.         fi.Success();
  115.     }
  116.     else
  117.         fi.ReSignal();
  118. }
  119.  
  120.  
  121. // TEenieView ----------------------------------------------------------------
  122.  
  123. #undef Inherited
  124. #define Inherited TView
  125.  
  126. #pragma segment AInit
  127. DefineClass(TEenieView, Inherited);
  128.  
  129.      TEenieView::TEenieView()
  130. {
  131.     
  132. }
  133.  
  134.  
  135. #pragma segment ARes
  136.      void TEenieView::DoPostCreate(TDocument* itsDocument)
  137. {
  138.     Inherited::DoPostCreate(itsDocument);
  139.     this->fCounter = 0L;                // zero counter
  140.     this->AddAdorner(NewStdAdorner('etch',"",'etch', kFreeOnDeletion), 
  141.                                     kAdornFirst, kRedraw);
  142. }
  143.  
  144.  
  145. #pragma segment Main
  146.      void TEenieView::Hilite()
  147. {
  148.     CRect tempRect;
  149.  
  150.     this->GetQDExtent(tempRect);        // get the area
  151.     LMSetHiliteMode(pHiliteBit);        // clear bit for selection color
  152.     HiliteColor(gDKPink);                // switch temp to new hilite color
  153.     InvertRect(tempRect);                // invert the area
  154. }
  155.  
  156.  
  157. // TEenieView SubViews ----------------------------------------------------------
  158.  
  159. #undef Inherited
  160. #define Inherited TEenieView
  161.  
  162. #pragma segment ARes
  163. DefineClass(TEenieView1, Inherited);
  164.  
  165.      void TEenieView1::Draw(const VRect& area)
  166. {    
  167.     Inherited::Draw(area);
  168.     this->DrawNumber();    
  169. }
  170.  
  171.  
  172. #pragma segment ARes
  173.      void TEenieView1::DrawNumber()
  174. {
  175.     CStr255 first = "Moo view clicked";
  176.     CStr255 count;
  177.     CStr255 second = "times...";
  178.     
  179.     PenNormal();
  180.     
  181.     TextFont(times); TextSize(14); MoveTo(10,15);
  182.     DrawString(first);
  183.  
  184.     TextSize(48); MoveTo(40,90);
  185.     NumToString(this->fCounter, count);
  186.     DrawString(count);
  187.     
  188.     TextSize(14); MoveTo(10,140);
  189.     DrawString(second);
  190. }
  191.  
  192. #pragma segment Main
  193.      void    TEenieView1::DoMouseCommand(VPoint&         /*theMouse*/,
  194.                                           TToolboxEvent*     /*event*/,
  195.                                           CPoint             /*hysteresis*/)
  196. {
  197.     this->Hilite();                    // flash for the user for mouse click
  198.     this->Changed(eVIEW1,this);        // send change directive to dependent view
  199. }
  200.  
  201. #pragma segment ARes
  202.      void TEenieView1::DoUpdate(ChangeID theID,
  203.                                 TObject* changedObject,
  204.                                 TObject* changedBy,
  205.                                 TDependencySpace* dependencySpace)
  206. {
  207.     if (theID == eVIEW4){
  208.         this->fCounter = this->fCounter + 1L;                // increment counter
  209.         this->ForceRedraw();                                // update the screen
  210.     }
  211. }
  212.  
  213. // --------------------------
  214.  
  215. #undef Inherited
  216. #define Inherited TEenieView
  217.  
  218. #pragma segment ARes
  219. DefineClass(TEenieView2, Inherited);
  220.  
  221.      void TEenieView2::Draw(const VRect& area)
  222. {    
  223.     Inherited::Draw(area);
  224.     this->DrawNumber();    
  225. }
  226.  
  227.  
  228. #pragma segment ARes
  229.      void TEenieView2::DrawNumber()
  230. {
  231.     CStr255 first = "Minee view clicked";
  232.     CStr255 count;
  233.     CStr255 second = "times...";
  234.     
  235.     PenNormal();
  236.     
  237.     TextFont(times); TextSize(14); MoveTo(10,15);
  238.     DrawString(first);
  239.  
  240.     TextSize(48); MoveTo(40,90);
  241.     NumToString(this->fCounter, count);
  242.     DrawString(count);
  243.     
  244.     TextSize(14); MoveTo(10,140);
  245.     DrawString(second);
  246. }
  247.  
  248.  
  249. #pragma segment Main
  250.      void    TEenieView2::DoMouseCommand(VPoint&         /*theMouse*/,
  251.                                           TToolboxEvent*  /*event*/,
  252.                                           CPoint             /*hysteresis*/)
  253. {
  254.     this->Hilite();                    // flash for the user for mouse click
  255.     this->Changed(eVIEW2,this);        // send change directive to dependent view
  256. }
  257.  
  258.  
  259. #pragma segment ARes
  260.      void TEenieView2::DoUpdate(ChangeID theID,
  261.                                 TObject* changedObject,
  262.                                 TObject* changedBy,
  263.                                 TDependencySpace* dependencySpace)
  264. {
  265.     if (theID == eVIEW3) {
  266.     this->fCounter = this->fCounter + 1L;                        // increment counter
  267.     this->ForceRedraw();                                        // update the screen
  268.     }
  269. }
  270.  
  271.  
  272. // --------------------------
  273.  
  274. #undef Inherited
  275. #define Inherited TEenieView
  276.  
  277. #pragma segment ARes
  278. DefineClass(TEenieView3, Inherited);
  279.  
  280.      void TEenieView3::Draw(const VRect& area)
  281. {    
  282.     Inherited::Draw(area);
  283.     this->DrawNumber();
  284. }
  285.  
  286.  
  287. #pragma segment ARes
  288.      void TEenieView3::DrawNumber()
  289. {
  290.     CStr255 first = "Meenie view clicked";
  291.     CStr255 count;
  292.     CStr255 second = "times...";
  293.     
  294.     PenNormal();
  295.     
  296.     TextFont(times); TextSize(14); MoveTo(10,15);
  297.     DrawString(first);
  298.  
  299.     TextSize(48); MoveTo(40,90);
  300.     NumToString(this->fCounter, count);
  301.     DrawString(count);
  302.     
  303.     TextSize(14); MoveTo(10,140);
  304.     DrawString(second);
  305. }
  306.  
  307.  
  308. #pragma segment Main
  309.      void    TEenieView3::DoMouseCommand(VPoint&         /*theMouse*/,
  310.                                           TToolboxEvent*     /*event*/,
  311.                                           CPoint             /*hysteresis*/)
  312. {
  313.     this->Hilite();                    // flash for the user for mouse click
  314.     this->Changed(eVIEW3,this);        // send change directive to dependent view
  315. }
  316.  
  317.  
  318. #pragma segment ARes
  319.      void TEenieView3::DoUpdate(ChangeID theID,
  320.                                 TObject* changedObject,
  321.                                 TObject* changedBy,
  322.                                 TDependencySpace* dependencySpace)
  323. {
  324.     if (theID == eVIEW2) { 
  325.     this->fCounter = this->fCounter + 1L;                // increment counter
  326.     this->ForceRedraw();                                // update the screen
  327.     }
  328. }
  329.  
  330.  
  331. // --------------------------
  332. #undef Inherited
  333. #define Inherited TEenieView
  334.  
  335. #pragma segment ARes
  336. DefineClass(TEenieView4, Inherited);
  337.  
  338.      void TEenieView4::Draw(const VRect& area)
  339. {    
  340.     Inherited::Draw(area);
  341.     this->DrawNumber();
  342. }
  343.  
  344. #pragma segment ARes
  345.      void TEenieView4::DrawNumber()        // draw information in view
  346. {
  347.     CStr255 first = "Eenie view clicked";
  348.     CStr255 count;
  349.     CStr255 second = "times...";
  350.     
  351.     PenNormal();
  352.     
  353.     TextFont(times); TextSize(14); MoveTo(10,15);
  354.     DrawString(first);
  355.  
  356.     TextSize(48); MoveTo(40,90);
  357.     NumToString(this->fCounter, count);
  358.     DrawString(count);
  359.     
  360.     TextSize(14); MoveTo(10,140);
  361.     DrawString(second);
  362. }
  363.  
  364.  
  365. #pragma segment Main
  366.      void    TEenieView4::DoMouseCommand(VPoint&         /*theMouse*/,
  367.                                           TToolboxEvent*     /*event*/,
  368.                                           CPoint             /*hysteresis*/)
  369. {
  370.     this->Hilite();                    // flash for the user for mouse click
  371.     this->Changed(eVIEW4,this);        // send change directive to dependent view
  372. }
  373.  
  374. #pragma segment ARes
  375.      void TEenieView4::DoUpdate(ChangeID theID,
  376.                                 TObject* changedObject,
  377.                                 TObject* changedBy,
  378.                                 TDependencySpace* dependencySpace)
  379. {
  380.     if (theID == eVIEW1){
  381.         this->fCounter = this->fCounter + 1L;        // increment counter
  382.         this->ForceRedraw();                        // update the screen
  383.     }
  384. }
  385.  
  386.  
  387. // TAdorners ------------------------------------------------------------------
  388.  
  389. #undef Inherited
  390. #define Inherited TAdorner
  391.  
  392. #pragma segment ARes
  393. DefineClass(TGrayFill, Inherited);
  394.  
  395.      void TGrayFill::Draw(TView* itsView, const VRect& /*area*/)
  396. {
  397.     CRGBColor    saveColor;
  398.     PenState    savePenState;
  399.     VRect        adornArea;
  400.     CRect        QDArea;
  401.     CRect        tempRect;
  402.  
  403.     GetPenState(&savePenState);            // save off the current pen state 
  404.     GetIfColor(saveColor);                // and the foreground color
  405.     PenNormal();
  406.  
  407.     itsView->GetAdornExtent(adornArea);    // get area
  408.     itsView->ViewToQDRect(adornArea, QDArea);
  409.     tempRect = QDArea;
  410.  
  411.     SetIfColor(gRGBGray);                // colorize it
  412.     PaintRect(tempRect);
  413.  
  414.     SetIfColor(saveColor);                // restore the foreground color and the pen
  415.     SetPenState(&savePenState);
  416. }
  417.  
  418.  
  419.  
  420. #undef Inherited
  421. #define Inherited TAdorner
  422.  
  423. #pragma segment ARes
  424. DefineClass(TOtherGrayFill, Inherited);
  425.  
  426.      void TOtherGrayFill::Draw(TView* itsView, const VRect& /*area*/)
  427. {
  428.     CRGBColor    saveColor;
  429.     PenState    savePenState;
  430.     VRect        adornArea;
  431.     CRect        QDArea;
  432.     CRect        tempRect;
  433.  
  434.     GetPenState(&savePenState);            // save off the current pen state 
  435.     GetIfColor(saveColor);                // and the foreground color
  436.     PenNormal();
  437.  
  438.     itsView->GetAdornExtent(adornArea);    // get area 
  439.     itsView->ViewToQDRect(adornArea, QDArea);
  440.     tempRect = QDArea;
  441.  
  442.     SetIfColor(gDKGray);                // dark gray scale, grey for you English
  443.     PaintRect(tempRect);
  444.  
  445.     SetIfColor(saveColor);                // Restore the foreground color and the pen
  446.     SetPenState(&savePenState);
  447. }
  448.  
  449.  
  450. #undef Inherited
  451. #define Inherited TAdorner
  452.  
  453. #pragma segment ARes
  454. DefineClass(TEtchedFrame, Inherited);
  455.  
  456.      TEtchedFrame::TEtchedFrame()    
  457. {
  458.     
  459.     fInset.h = 1;
  460.     fInset.v = 1;
  461. }
  462.  
  463.  
  464. #pragma segment ARes
  465.      void TEtchedFrame::Draw(TView* itsView, const VRect&    /*area*/)
  466. {
  467.     CRGBColor    saveColor;
  468.     PenState    savePenState;
  469.     VRect         adornArea;
  470.     CRect        QDArea;
  471.     CRect        tempRect;
  472.  
  473.     GetPenState(&savePenState);            //  Save off the current pen state 
  474.     GetIfColor(saveColor);                // and the foreground color
  475.     PenNormal();
  476.  
  477.     itsView->GetAdornExtent(adornArea);    // get area
  478.     itsView->ViewToQDRect(adornArea, QDArea);
  479.     tempRect = QDArea;
  480.  
  481.     SetIfColor(gRGBBlack);
  482.     InsetRect(tempRect, fInset.h, fInset.v);
  483.     FrameRect(tempRect);                                    // Frame the shadows 
  484.  
  485.     OffsetRect(tempRect, 1, 1);
  486.     SetIfColor(gRGBWhite);
  487.     FrameRect(tempRect);                                    // Frame the light edges 
  488.  
  489.     OffsetRect(tempRect, -1, -1);
  490.     SetCPixel(tempRect.left, tempRect.bottom, gRGBBlack);    // Finish the corners 
  491.     SetCPixel(tempRect.right, tempRect.top, gRGBBlack);
  492.  
  493.     SetIfColor(saveColor);                // Restore the foreground color and the pen
  494.     SetPenState(&savePenState);
  495. }
  496.  
  497.  
  498.